package com.syezon.wifikey.wxapi;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.syezon.android.base.utils.ImageUtils;
import com.syezon.wifikey.R;
import com.syezon.wifikey.utils.Utils;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WxShare {

    private static final int THUMB_SIZE = 150;

    private static IWXAPI iwxapi;

    static {
        iwxapi = WXAPIFactory.createWXAPI(Utils.getApp(), WXConstant.APP_ID);
    }

    /**
     * 微信回话
     *
     * @param url
     * @param title
     * @param description
     * @param transaction
     */
    public static void wxSceneSession(String url, String title, String description, String transaction) {
        try {
            WXWebpageObject webpage = new WXWebpageObject();
            webpage.webpageUrl = url;
            WXMediaMessage msg = new WXMediaMessage(webpage);
            msg.title = title;
            msg.description = description;
            Bitmap bmp = BitmapFactory.decodeResource(Utils.getApp().getResources(), R.mipmap.ic_launcher);
            Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
            bmp.recycle();
            msg.thumbData = ImageUtils.bitmap2Bytes(thumbBmp, Bitmap.CompressFormat.PNG);

            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = transaction;
            req.message = msg;
            req.scene = SendMessageToWX.Req.WXSceneSession;
            iwxapi.sendReq(req);
        } catch (Exception e) {

        }
    }

    public static void wxPYSession(String url, String title, String description, String transaction) {
        try {
            WXWebpageObject webpage = new WXWebpageObject();
            webpage.webpageUrl = url;
            WXMediaMessage msg = new WXMediaMessage(webpage);
            msg.title = title;
            msg.description = description;
            Bitmap bmp = BitmapFactory.decodeResource(Utils.getApp().getResources(), R.mipmap.ic_launcher);
            Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
            bmp.recycle();
            msg.thumbData = ImageUtils.bitmap2Bytes(thumbBmp, Bitmap.CompressFormat.PNG);
            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = transaction;
            req.message = msg;
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
            iwxapi.sendReq(req);
        } catch (Exception e) {

        }
    }
}
